package org.robot.scm.pojos.platform;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.robot.scm.pojos.BaseNotify;
import org.robot.scm.pojos.TargetNotify;
import org.robot.scm.pojos.config.BaseConfig;
import org.robot.scm.pojos.enums.ChannelEnum;
import org.robot.scm.pojos.enums.HttpMethodEnum;

@Data
@Builder
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class RobotBaseNotify extends BaseNotify {

    private String messageId;

    private String content;

    private String toUserId;

    @SneakyThrows
    @Override
    public TargetNotify robotToNotifyBean(String robotId, BaseConfig config) {
        String formatContent = config.robotText(toUserId, URLEncoder.encode(content, StandardCharsets.UTF_8));
        return TargetNotify.builder()
                .httpMethodEnum(HttpMethodEnum.POST)
                .channelEnum(ChannelEnum.ROBOT)
                .notifyUrl(config.robotUrl())
                .topic(config.get().getNotifyChannel().getRobot().getBodyText())
                .content(formatContent)
                .build();
    }

    @Override
    public boolean equal(BaseNotify another) {
        return StringUtils.equals(((RobotBaseNotify) another).getMessageId(), messageId);
    }

}
